var LogEnum = {
    ERROR: "[error] - ",
    INFO: "[info] - "
};

Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
};

var filePath = "/sdcard/baishuiFile/";

filePreConfig = function (fileName) {
    var t = file.exists(filePath);
    if (!t) {
        var mk = file.mkdirs(filePath);
        if (mk) {
            logd("创建文件夹成功。。。")
        }
    }
    var todayFilePath = filePath + fileName + ".txt";
    var exists = file.exists(todayFilePath);
    if (!exists) {
        // 创建文件
        var create = file.create(todayFilePath);
        if (create) {
            logd("创建文件 " + todayFilePath + " 成功。。。")
        }
    }
    return todayFilePath;
};

getNowTime = function () {
    return "[" + new Date().format("yyyy-MM-dd hh:mm:ss") + "] - ";
};

afterNeedToExit = function (str) {
    var date = new Date().format("yyyy-MM-dd").replace(/-/g, "");
    if (date > str) {
        loge("过期啦。。。")
        exit();
    }
};

/**
 * 统一记录日志的
 * @param logEnum LogEnum
 * @param str str
 */
logToFile = function (logEnum, str) {
    var fileName = new Date().format("yyyy-MM-dd");
    if (logEnum != null) {
        switch (logEnum) {
            case LogEnum.ERROR:
                str = LogEnum.ERROR + getNowTime() + str;
                break;
            case LogEnum.INFO:
                str = LogEnum.INFO + getNowTime() + str;
                break;
            default:
                break
        }
        file.appendLine(str, filePreConfig(fileName));
    } else {
        loge("log param error，need LogEnum, pls check")
    }
};

/**
 * 每次操作结束后就记录一下
 *
 * @param array 数组
 */
logToClickedMember = function (array) {
    var str = "";
    if (array != null) {
        var num = array[array.length - 1]["id"];
        var name = array[array.length - 1]["name"];
        str = "已经成功为第 [" + num + "] 位点击完成啦, 他的名字叫：[" + name + "]";
    }
    logToBaseClickedMember(str);
};

/**
 * 基类
 *
 * @param str
 */
logToBaseClickedMember = function (str) {
    var fileName = new Date().format("yyyy-MM-dd") + "-clickedMember";
    file.appendLine(getNowTime() + str, filePreConfig(fileName));
};

/**
 * 每次任务启动都需要执行一次
 */
logToStart = function () {
    var str = "=============> task is ready to Go GO GO <================";
    logToFile(LogEnum.INFO, str);
    logToBaseClickedMember(str)
};

/**
 * 任务结束执行
 */
logToEnd = function () {
    var str = "=============> completion of task <================";
    logToFile(LogEnum.INFO, str);
    logToBaseClickedMember(str)
};
/**
 * var time1 = new Date().format("yyyy-MM-dd hh:mm:ss");
 console.log(time1);


 var time2 = new Date().format("yyyy-MM-dd");
 console.log(time2);

 var oldTime = (new Date("2012/12/25 20:11:11")).getTime();
 var curTime = new Date(oldTime).format("yyyy-MM-dd");
 console.log(curTime);
 */